package com.gitee.wsl.compose.mvi.router.stack

import com.gitee.wsl.compose.mvi.router.RouteNode
import com.gitee.wsl.compose.mvi.router.Router


 data class Stack<out C : Any>(
    val active: RouteNode<C>,
    val inactive: List<RouteNode<C>>
)

 class StackRouter<R : Any>(name: String) : Router<TransformStack<R>>(name)

 typealias TransformStack<R> = (routes: List<R>) -> List<R>

 fun <R : Any> StackRouter<R>.push(route: R) {
    route { routes -> routes + route }
}

 fun <R : Any> StackRouter<R>.pop(onLastItem: () -> Unit) {
    route { routes ->
        if (routes.size > 1) {
            routes.subList(0, routes.size - 1)
        } else {
            onLastItem()
            routes
        }
    }
}

 fun <R : Any> StackRouter<R>.replace(route: R) {
    route { routes ->
        routes.subList(0, routes.size - 1) + route
    }
}

 fun <R : Any> StackRouter<R>.replaceAll(route: R) {
    route { listOf(route) }
}